<?php
namespace  app\admin\controller;
use app\admin\model\Admin_model;
use think\Controller;
use think\Db;
use think\Request;

Class Advertising extends  Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('active','我的广告');
            $this->assign('fun',$fun);
        }else{
            $this->redirect('index/login/index');
        }
    }
    //广告组
    public function  adv_group(){
        $user=session('user');
        if($user['role']==DLS){
            $advertisers=Db::name('advertisers')->where('attr_user_id',$user['id'])->alias('a')->field('a.*,b.id as user_id')
                ->join('user b','a.name=b.username')->select();
        }elseif ($user['role']==GGZ){
            $advertisers=Db::name('advertisers')->where('a.name',$user['username'])->alias('a')->field('a.*,b.id as user_id')
                ->join('user b','a.name=b.username')->select();
        }
        $this->assign('advertisers',$advertisers);
        $this->assign('title','广告组');
        return $this->fetch();
    }
    public function  adv_group_list(){
        $where=array();
        $user=session('user');
        if($user['role']==DLS){
            $where['a.attr_agent_user_id']=$user['id'];
        }
        else if($user['role']==GGZ){
            $where['a.attr_adv_user_id']=$user['id'];
        }
        if(isset($_GET['attr_adv_user_id'])&&$_GET['attr_adv_user_id']){
            $where['a.attr_adv_user_id']=$_GET['attr_adv_user_id'];
        }
        if(isset($_GET['search'])&&$_GET['search']){
            $where['a.group_name']=['like','%'.$_GET['search'].'%'];
        }
        if(isset($_GET['state'])&&$_GET['state']){
            $total=Db::name('adv_group')->alias('a')
                ->field('a.*,b.attr_adv_group_id,SUM(b.show_num) as show_num,SUM(b.click_num) as click_num,SUM(b.cpm_amount) as cpm_amount,SUM(b.cpc_amount) as cpc_amount')
                ->join('adv_statistical b','a.id=b.attr_adv_group_id','left')
                ->where($where)
                ->group('a.id')->count();
            $data=Db::name('adv_group')->alias('a')
                ->field('a.*,b.attr_adv_group_id,SUM(b.show_num) as show_num,SUM(b.click_num) as click_num,SUM(b.cpm_amount) as cpm_amount,SUM(b.cpc_amount) as cpc_amount')
                ->join('adv_statistical b','a.id=b.attr_adv_group_id','left')
                ->where($where)->limit($_GET['offset'],$_GET['limit'])
                ->group('a.id')->select();
            foreach ($data as &$k){
                $k['sum_amount']=$k['cpm_amount']+$k['cpc_amount'];
                if($k['show_num']){
                    $k['click_rate']=round($k['click_num']/$k['show_num']*100,2);
                    $k['CPM']=round($k['sum_amount']*1000/$k['show_num'],4);
                    $k['CPC']=round($k['sum_amount']/$k['click_num'],4);
                }
            }
        }else{
            $total=Db::name('adv_group')->alias('a')->where($where)->count();
            $data=Db::name('adv_group')->alias('a')->where($where)->limit($_GET['offset'],$_GET['limit'])->select();
        }
        foreach ($data as &$k){
            $k['operation']='<a onclick="del_group('.$k['id'].')" class="btn btn-danger">删除</a> ';
        }
        return array('rows'=>$data,'total'=>$total);
    }
    public  function  del_group(){
        $group_id=$_GET['id'];
        $info= Db::name('adv_group')->where('id',$group_id)->delete();
        if($info){
            $plan_info= Db::name('adv_plan')->where('attr_adv_group_id',$group_id)->delete();
            if($plan_info){
                return array('code' => 1, 'msg' => '删除成功');
            }else{
                return array('code' => 0, 'msg' => '删除失败');
            }
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //广告计划
    public function  adv_plan(){
        $this->assign('title','广告计划');
        return $this->fetch();
    }
    public function  adv_plan_list(){
        $where=array();
        $user=session('user');
        if($user['role']==DLS){
            $where['a.attr_agent_user_id']=$user['id'];
        }
        else if($user['role']==GGZ){
            $where['a.attr_adv_user_id']=$user['id'];
        }
        if($_GET['search']){
            $where['a.plan_name']=['like','%'.$_GET['search'].'%'];
        }
        if($_GET['state']){
            $total=Db::name('adv_plan')->alias('a')->where($where)->count();
            $data=Db::name('adv_plan')->alias('a')
                ->field('a.*,b.attr_adv_group_id,b.show_num as num_show,b.click_num as num_click,b.cpm_amount,b.cpc_amount')
                ->join('adv_statistical b','a.id=b.adv_plan_id','left')
                ->where($where)->limit($_GET['offset'],$_GET['limit'])->order('a.id','asc')->select();
            foreach ($data as &$k){
                $k['sum_amount']=$k['cpm_amount']+$k['cpc_amount'];
                if($k['num_show']){
                    $k['click_rate']=round($k['num_click']/$k['num_show']*100,2);
                    $k['CPM']=round($k['sum_amount']*1000/$k['num_show'],4);
                    $k['CPC']=round($k['sum_amount']/$k['num_click'],4);
                }
            }
        }else{
            $total=Db::name('adv_plan')->alias('a')->where($where)->count();
            $data=Db::name('adv_plan')->alias('a')->where($where)->limit($_GET['offset'],$_GET['limit'])->select();
        }
        $audit=array(0=>'待审核',1=>'待投放',2=>'未通过审核',3=>'投放中');
        $pay_type=array(0=>'CPM',1=>'CPC');
        $budget_type=array(0=>'总预算', 1=>'日预算');
        $styles=array();
        $styles[0]='';
        $adv_style=Db::name('adv_style')->select();

        foreach ($adv_style as $style){
            $styles[$style['id']]=$style['style_name'];
        }
        foreach ($data as &$k){
            $k['audit_state']=$audit[$k['audit_state']];
            $k['style']=$styles[$k['style']];
            $k['pay_type']=$pay_type[$k['pay_type']];
            $k['budget_type']=$budget_type[$k['budget_type']];
            if($k['put_type']==0){
                $k['put_end_time']='长期投放';
            }
            $k['operation']='<div style="width: 180px;"><a href="'.url('edit_adv_plan').'?id='.$k['id'].'" class="btn btn-info">编辑</a>
|<a href="'.url('edit_adv_plan').'?id='.$k['id'].'" class="btn btn-default">复制</a>
|<a onclick="del_adv_plan('.$k['id'].')" class="btn btn-danger">删除</a></div> ';
        }
        return array('rows'=>$data,'total'=>$total);
    }
    public function  del_adv_plan(){
        $plan_id=$_GET['id'];
        $info= Db::name('adv_plan')->where('id',$plan_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //获取广告组信息
    public  function get_group(){
        $id=$_GET['g_id'];
        $group=Db::name('adv_group')->where('id',$id)->find();
        return $group;
    }
    //获取该样式具体信息
    public  function get_adv_location(){
        $id=$_GET['s_id'];
        $loc=array();
        $style=Db::name('adv_style')->where('id',$id)->find();
        if ($style){
            $loc['adv_location']=explode(',',$style['style_put_site']);
            $loc['state']=$style['title_state'];
            return $loc;
        }else{
            return null;
        }
    }
    //添加广告
    public function  add_adv_plan(){
        $user=session('user');
        if($user['role']==DLS){
           $group= Db::name('adv_group')->where('attr_agent_user_id',$user['id'])->select();
           $advertisers=Db::name('advertisers')->where('attr_user_id',$user['id'])->alias('a')->field('a.*,b.id as user_id')
               ->join('user b','a.name=b.username')->select();
        }elseif ($user['role']==GGZ){
            $group= Db::name('adv_group')->where('attr_adv_user_id',$user['id'])->select();
            $advertisers=Db::name('advertisers')->where('a.name',$user['username'])->alias('a')->field('a.*,b.id as user_id')
                ->join('user b','a.name=b.username')->select();
        }
        //获取广告分类
        $industry= Db::name('industry')->where('level',1)->select();
        $this->assign('industry',$industry);
        //获取自定义渠道
        $channel=Db::name('channel')->select();
        $this->assign('channel',$channel);
        //获取指定自定义手机品牌
        $brand=Db::name('phone_brand')->select();
        $this->assign('brand',$brand);
        //获取指定阅读兴趣定向
        $interest=Db::name('interest')->select();
        $this->assign('interest',$interest);
        //获取app自定义定向
        $app_custom=Db::name('app_custom')->select();
        $this->assign('app_custom',$app_custom);
        //获取按app定向
        $apps=Db::name('app')->select();
        $this->assign('apps',$apps);
        //获取自定义安装应用偏好
        $apply_bias=Db::name('apply_bias')->select();
        $this->assign('apply_bias',$apply_bias);
        //获取自定义广告点击偏好
        $click_bias=Db::name('click_bias')->select();
        $this->assign('click_bias',$click_bias);
        //获取广告样式
        $style=Db::name('adv_style')->select();
        $this->assign('style',$style);

        $this->assign('advertisers',$advertisers);
        $this->assign('group',$group);
        $this->assign('title','新建广告');
        return $this->fetch();
    }
    public function  edit_adv_plan(){
        $plan_id=$_GET['id'];
        $plan=Db::name('adv_plan')->where('id',$plan_id)->find();
        $this->assign('plan',$plan);
        $user=session('user');
        //获取广告组与广告主
        if($user['role']==DLS){
            $group= Db::name('adv_group')->where('attr_agent_user_id',$user['id'])->select();
            $advertisers=Db::name('advertisers')->where('attr_user_id',$user['id'])->alias('a')->field('a.*,b.id as user_id')
                ->join('user b','a.name=b.username')->select();
        }elseif ($user['role']==GGZ){
            $group= Db::name('adv_group')->where('attr_adv_user_id',$user['id'])->select();
            $advertisers=Db::name('advertisers')->where('a.name',$user['username'])->alias('a')->field('a.*,b.id as user_id')
                ->join('user b','a.name=b.username')->select();
        }
        $this->assign('advertisers',$advertisers);
        $this->assign('group',$group);
        //获取广告分类
        $industry= Db::name('industry')->where('level',1)->select();
        $this->assign('industry',$industry);
        //获取自定义渠道
        $channel=Db::name('channel')->select();
        $this->assign('channel',$channel);
        //获取指定自定义手机品牌
        $brand=Db::name('phone_brand')->select();
        $this->assign('brand',$brand);
        //获取指定阅读兴趣定向
        $interest=Db::name('interest')->select();
        $this->assign('interest',$interest);
        //获取app自定义定向
        $app_custom=Db::name('app_custom')->select();
        $this->assign('app_custom',$app_custom);
        //获取按app定向
        $apps=Db::name('app')->select();
        $this->assign('apps',$apps);
        //获取自定义安装应用偏好
        $apply_bias=Db::name('apply_bias')->select();
        $this->assign('apply_bias',$apply_bias);
        //获取自定义广告点击偏好
        $click_bias=Db::name('click_bias')->select();
        $this->assign('click_bias',$click_bias);    
        //获取广告样式
        $style=Db::name('adv_style')->select();
        $this->assign('style',$style);

        $this->assign('title','编辑广告');
        return $this->fetch();
    }
    public function  add_plan_check(){
        $user=session('user');
        $data=input('post.');
        $group=array();
        $paln_code= strtotime('now').$user['id'];//生成广告计划ID
        if(!isset($data['attr_adv_group_id'])) {
            $advertisers=Db::name('advertisers')->alias('a')->field('a.*,b.id as user_id')
                ->join('user b','a.name=b.username')->where('b.id',$data['attr_adv_user_id'])->find();
            $group['group_name'] = $data['group_name'];
            $group['group_budget_type'] = $data['group_budget_type'];
            $group['amount'] = $data['amount'];
            $group['platform'] = $data['group_platform'];
            $group['attr_adv_user_id'] = $advertisers['user_id'];
            $group['attr_adv_name'] = $advertisers['advertisers_name'];
            $group['attr_agent_user_id'] =  $advertisers['attr_user_id'];
            $id = Db::name('adv_group')->insertGetId($group);
            if($id){
                $data['attr_adv_group_id']=$id;
                $data['attr_adv_group_name']=$group['group_name'];
                $data['attr_adv_user_name']=$group['attr_adv_name'];
                $data['attr_adv_user_id']=$group['attr_adv_user_id'];
                $data['attr_agent_user_id']=$group['attr_agent_user_id'];
            }else{
                  return array('code' => 0, 'msg' => '新建广告组失败');
            }
            unset($data['group_name']);
            unset($data['group_budget_type']);
            unset($data['amount']);
            unset($data['group_platform']);

        }else{
            $group=Db::name('adv_group')->where('id',$data['attr_adv_group_id'])->find();
            $data['attr_adv_group_name']=$group['group_name'];
            $data['attr_adv_user_name']=$group['attr_adv_name'];
            $data['attr_adv_user_id']=$group['attr_adv_user_id'];
            $data['attr_agent_user_id']=$group['attr_agent_user_id'];

        }
        if (isset($data['id'])){
            $info=Db::name('adv_plan')->where('id',$data['id'])->update($data);
            if($info){
                return array('code' => 1, 'msg' => '更新广告计划成功','url'=>url('adv_plan'));
            }else{
                return array('code' => 0, 'msg' => '更新广告计划失败');
            }
        }else{
            $data['plan_code']=$paln_code;
            $info=Db::name('adv_plan')->insertGetId($data);
            if($info){
                return array('code' => 1, 'msg' => '新建广告计划成功','url'=>url('adv_plan'));
            }else{
                return array('code' => 0, 'msg' => '新建广告计划失败');
            }
        }
    }
}